﻿
namespace CID.Domain
{
    static class EntityEqualityHelper
    {
        public static bool Equals(IEntity entity1, object entity2)
        {
            var result = ReferenceEquals(entity1, entity2);
            if(!result && (entity2 is IEntity)) result = Equals(entity1, entity2 as IEntity);
            return result;
        }

        public static bool Equals(IEntity entity1, IEntity entity2)
        {
            var sameIds = (entity1.Id == entity2.Id);
            var notSavedEntities = (sameIds && (entity1.Id == 0));
            return (notSavedEntities && ReferenceEquals(entity1, entity2)) || (!notSavedEntities && entity1.GetType().IsAssignableFrom(entity2.GetType()) && sameIds);
        }
    }
}
